perm filename EFTPR.C[11,HE] blob sn#688197 filedate 1982-12-06 generic text, type T, neo UTF8
/* LINTLIBRARY */
/*
 * eftpread.c
 *
 * EFTP Package
 *
 * EftpRead -- read an arbitrary buffer from an eftp channel
 *
 * Jeffrey Mogul @ Stanford	13-February-1981
 */

#include <eftp.h>
#include <puplib.h>
#include <pupstatus.h>

#define min(a,b)  ( (a<b)? a:b )

EftpRead(EfChan,buffer,buflen,rbuflen)
struct EftpChan *EfChan;	/* eftp channel to read from */
char *buffer;			/* buffer to read */
int buflen;			/* maximum length of buffer */
int *rbuflen;			/* returned length of buffer */
{	/* */
	char	swapbuf[EFTP_MAX_PACKET];	/* for byte-swapping */
	int	ThisChunk;	/* size of current packet */
	int	status;		/* status from EfRecPckt */
	int	retlen;		/* returned buffer length */
	int	swapbytes;	/* true iff we should swab instead of bmove */


	status = EfRecPckt(EfChan, swapbuf, &retlen);	/* get packet */
	retlen = min(buflen, retlen);	/* don't return more than asked for */
	
#ifdef	PUP__NNSO	/* nonstandard machine (eg, VAX) */

	swapbytes = ! (EfChan->ef_mode&EFTPM_BYTESWAP);
			/* byteswap means maintain NSO */

#else			/* standard machine (e.g., MC68000) */

	swapbytes =  (EfChan->ef_mode&EFTPM_BYTESWAP);
			/* byteswap means defeat NSO */

#endif

/* 	"status" now holds returned status from EfRecPckt */
	switch ((int)status) {

		case OK:	/* it worked -- return buffer to user */
		case EFTP_RESTART:	/* ... but inform of a restart */
			if (swapbytes)
				swab(swapbuf, buffer, retlen);
			else	/* don't byteswap, just move it */
				bmove(swapbuf, buffer, retlen);
			*rbuflen = retlen;	/* return len. of good buffer */
			return(status);	/* OK or EFTP_RESTART */
		
		case EFTP_ENDOFFILE:	/* end of file */
			*rbuflen = 0;
			return(EFTP_ENDOFFILE);

		case EFTP_ABORT:	/* some sort of abort */
			*rbuflen = 0;	/* return nothing */
			switch ((int)EftpAbortCode) { /* what sort? */
			    case EFTPA_RECBUSY:		/* Receiver Busy */
				EftpWaitTime = EFTPW_RBUSY;
				return(EFTP_RESTART);

			    case EFTPA_LONGWAIT:	/* Long Wait */
				EftpWaitTime = EFTPW_LWAIT;
				return(EFTP_RESTART);

			    case EFTPA_MEDWAIT:		/* Medium Wait */
				EftpWaitTime = EFTPW_MWAIT;
				return(EFTP_RESTART);

			    default:	/* nasty abort */
				return(EFTP_ABORT);
			    }  /* end switch on abort code */

		case TIMEOUT:	/* timed out */
		case EFTP_OUTOFSYNCH:	/* sender out of synch */
		case EFTP_ERROR:	/* got SOMETHING nasty */
		default:		/* should never happen? */
			*rbuflen = 0;	/* return nothing */
			return(status);
		}	/*end switch on return from EfRecPckt */
}